package com.cskaoyan.config5;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/08/11 10:42
 */
@Configuration
@EnableConfigurationProperties(DatasourceProperties.class)
// value属性要写包含@ConfigurationProperties的类的Class → 把这个参数类注册为容器中的组件
public class DataSourceConfiguration5 {

    DatasourceProperties properties;
    // 有参构造方法来完成实例化 → 形参，从容器中默认按照类型取出组件
    public DataSourceConfiguration5(DatasourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource5() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
